<?php

  error_reporting(E_ALL^E_NOTICE^E_WARNING);

  $MySQLMainDB = 'main';

  include_once "../config.php";
  include_once __CFG_PATH_CODE_ADMIN."loader.php";
  header('Content-type: text/html; charset='.$language['charset']);

  $ADMIN = $sID->fetch('admin');
  $tpl->assign('Admin', $ADMIN);

  /** Checkout system login */
  checkAdminPage();

  if (empty($_REQUEST['mode'])) $_REQUEST['mode'] = $ADMIN['AdmMode'];
  if (empty($_REQUEST['mode'])) $_REQUEST['mode'] = "logout";

  $TabFields['amg'] = getFieldNamesWithLangs($_SQL_TABLE['admin_menu_group'], array('title'));
  $TabFields['am'] = getFieldNamesWithLangs($_SQL_TABLE['admin_menu'], array('title'));

  if($sID->fetch('isGlobalAdmin') == __TRUE)
  {  	$query = "
  			SELECT 	amg.".$TabFields['amg']['title']." AS groupTitle,
  					amg.code AS groupCode,
  					am.".$TabFields['am']['title']." AS menuTitle,
  					am.linkvar AS menuLinkvar,
  					am.addlinkvars AS menuAddlinkvars
  			FROM ".$_SQL_TABLE['admin_menu']." am
			INNER JOIN ".$_SQL_TABLE['admin_menu_group']." amg ON amg.code = am.menu_group AND amg.active = 'y'
			WHERE am.active = 'y'
			ORDER BY amg.sorting,am.sorting
    ";
  }
  else
  {
    $query = "
  			SELECT 	amg.".$TabFields['amg']['title']." AS groupTitle,
  					amg.code AS groupCode,
  					am.".$TabFields['am']['title']." AS menuTitle,
  					am.linkvar AS menuLinkvar,
  					am.addlinkvars AS menuAddlinkvars
  			FROM ".$_SQL_TABLE['admin_menu_user_group']." amug
			INNER JOIN ".$_SQL_TABLE['admin_menu']." am ON am.id = amug.admin_menu AND am.active = 'y'
			INNER JOIN ".$_SQL_TABLE['admin_menu_group']." amg ON amg.code = am.menu_group AND amg.active = 'y'
			WHERE amug.active = 'y'
			  AND amug.user_group = '".$ADMIN['AdmGroup']."'
			ORDER BY amg.sorting,am.sorting
    ";
  }

  $dbSet->open($query);
  $AdmMenu = $dbSet->fetchRowsAll();

  foreach($AdmMenu as $n => $arr)
  {	if(!is_array($adminMenu[$arr['groupCode']]))
	{
	  $adminMenu[$arr['groupCode']]['title'] = $arr['groupTitle'];
	  $adminMenu[$arr['groupCode']]['code'] = $arr['groupCode'];
	}
	$adminMenu[$arr['groupCode']]['items'][] = array('title' => $arr['menuTitle'], 'linkvar' => $arr['menuLinkvar'], 'addlinkvars' => $arr['menuAddlinkvars']);

  }

  $tpl->assign('adminMenu', $adminMenu);
  $tpl->assign('adminMode', $_REQUEST['mode']);
  require_once "config_sql_tab.php";


  foreach($AdmMenu as $n => $arr)
  {    if(preg_match("/index\.php\?mode\=".$arr['menuLinkvar']."[^\?]*".$arr['menuAddlinkvars']."/Uis", $_SERVER['REQUEST_URI']))
    {
      $AccessFlag = __TRUE;
    }
  }
  if($AccessFlag != __TRUE && $_REQUEST['mode'] != 'main' && $_REQUEST['mode'] != 'login' && $_REQUEST['mode'] != 'logout' && $_REQUEST['mode'] != 'get_values' && $sID->fetch('isGlobalAdmin') == false)
  {
    include_once __CFG_PATH_CODE_ADMIN."admin.noaccess.php";
  }
  elseif(is_file(__CFG_PATH_CODE_ADMIN."admin.".$_REQUEST['mode'].".php"))
  {
    include_once __CFG_PATH_CODE_ADMIN."admin.".$_REQUEST['mode'].".php";
  }
  elseif(is_file(__CFG_PATH_CODE_ADMIN."admin.".str_replace($CONFIG['adminListPart'], "", $_REQUEST['mode']).".php"))
  {
    go_to(str_replace($CONFIG['adminListPart'], "", getSameUri()));
    exit;
  }
  elseif(!is_file(__CFG_PATH_CODE_ADMIN."admin.".$_REQUEST['mode'].".php") && isBlank(strstr($_REQUEST['mode'], $CONFIG['adminListPart'])) && !isEmptyArr($_SQL_TABLE_FIELDS[$GlobPart]))
  {  	include_once __CFG_PATH_CODE_ADMIN."admin.item.php";
  }
  elseif(!is_file(__CFG_PATH_CODE_ADMIN."admin.".$_REQUEST['mode'].".php") && !isBlank(strstr($_REQUEST['mode'], $CONFIG['adminListPart'])) && !isEmptyArr($_SQL_TABLE_FIELDS[$GlobPart]))
  {
  	stripslashesResult($_POST,$recursive=1);
  	include_once __CFG_PATH_CODE_ADMIN."admin.items.php";
  }
  else
  {  	stripslashesResult($_POST,$recursive=1);
  	include_once __CFG_PATH_CODE_ADMIN."admin.main.php";
  }

  $tpl->display($_ADMIN_SMARTY_TEMPLATE);
  $Timer->stop("common");
  //$Timer->getTimerInfo();
  //$Timer->display();

?>